package codeRandomThoughts.Test225用队列实现栈;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 队列模拟栈比较绕
 * 要记住有一个队列是无用的,主要用来过度
 * 如果用单个队列,需要知道队列的大小 size
 */
public class MyStack {
    Queue<Integer> queue;

    public MyStack() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.offer(x);
    }

    public int pop() {
        int size = queue.size();
        //把size-1个元素poll再offer
        for (int i = 1; i <= size - 1; i++) {
            Integer poll = queue.poll();
            queue.offer(poll);
        }

        return queue.poll();
    }

    public int top() {
        int pop = this.pop();
        queue.offer(pop);
        return pop;
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}
